package com.atguigu;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Set;

public class JedisPoolDemo {
    private static JedisPool jedisPool = null;

    //使用单例的方式创建连接池
    public static Jedis getJeids(String host){
        //先判断有没有连接池
        if (jedisPool==null){
            //主要配置
            JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(10); //最大可用连接数
            jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
            jedisPoolConfig.setMinIdle(5); //最小闲置连接数
            jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
            jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
            jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong

            jedisPool = new JedisPool(jedisPoolConfig,host);
        }
        return jedisPool.getResource();
    }

    public static void main(String[] args) {
        Jedis jeids = getJeids("hadoop102");

        Set<String> shuihuzhuan = jeids.smembers("shuihuzhuan");
        for (String s : shuihuzhuan) {
            System.out.println(s);
        }

        //关闭连接
        jeids.close();
    }

}
